package org.m3;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class Quiz extends Activity implements OnClickListener {
    private int index = 0;
    private List<Animals> animalsList;
    private TextView name;
    private ImageView image1;
    private ImageView image2;
    private MediaPlayer mp;
    private Button btnGameList;
    private Button btnSpeaker;
    private Button btnNext;
    private Dialog dlgCorrect;
    private Dialog dlgWrong;
    private String question;
    private String dQuestion;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L10N.init(this.getResources());
        setContentView(R.layout.quiz);
        
        initBtnGameList();
        initBtnSpeaker();
        initBtnNext();
        
        animalsList = Arrays.asList(Animals.values());
        Collections.shuffle(animalsList);
        
        nextAnimal();
	}

    @Override
    public void onClick(View v) {
    	if(mp!=null) {
    		mp.release();
    	}
    	if(v.equals(image1)) {
    		getDlgCorrect().show();
            new Thread(new Runnable() {
				@Override
				public void run() {
					try {
		            	Thread.sleep(2000);
		            }catch(InterruptedException e) {
		            }
		            getDlgCorrect().dismiss();
				}
            }).start();
            if(++index >= Animals.values().length) index = 0;
        	nextAnimal();            
    	} else if(v.equals(getImage2())) {
    		getDlgWrong().show();
            new Thread(new Runnable() {
				@Override
				public void run() {
					try {
		            	Thread.sleep(2000);
		            }catch(InterruptedException e) {
		            }
		            getDlgWrong().dismiss();
		            playSound();            
				}
            }).start();
    	} else if(v.equals(btnGameList)) {
			finish();
    	} else if(v.equals(btnSpeaker)) {
    		Home.soundEnabled = !Home.soundEnabled;
    		btnSpeaker.setCompoundDrawablesWithIntrinsicBounds(null, this.getResources().getDrawable(Home.soundEnabled ? R.drawable.speaker : R.drawable.dspeaker), null, null);
    		nextAnimal();
    	} else if(v.equals(btnNext)) {
        	if(++index >= Animals.values().length) index = 0;
            nextAnimal();
    	}
    }

    
    // PRIVATE METHODS
    
    private TextView getName() {
    	if(name == null) {
    		name = (TextView) findViewById(R.id.name);
    		name.setTextAppearance(this, R.style.question);
            name.setTypeface(L10N.font);
        }
    	return name;
    }
    
    private ImageView getImage1() {
    	if(image1 == null) {
    		image1 = (ImageView) findViewById(R.id.image1);
    		image1.setOnClickListener(this);
        }
    	return image1;
    }
    
    private ImageView getImage2() {
    	if(image2 == null) {
    		image2 = (ImageView) findViewById(R.id.image2);
    		image2.setOnClickListener(this);
        }
    	return image2;
    }
    
    private String getQuestion() {
    	if(question == null) {
    		question = getResources().getString(R.string.question);
    	}
    	return question;
    }
    
    private String getDQuestion() {
    	if(dQuestion == null) {
    		dQuestion = getResources().getString(R.string.dquestion);
    	}
    	return dQuestion;
    }
    
	private void nextAnimal() {
		String s = Home.soundEnabled ? getQuestion() : (getDQuestion() + ' ' + getResources().getString(animalsList.get(index).getName())); 
		getName().setText(s);
        
		Drawable i1 = getResources().getDrawable(animalsList.get(index).getImage()); 
		Drawable i2 = getResources().getDrawable(getRandomDrawable());
		Drawable[] images = new Drawable[]{i1, i2};
        List<Drawable> random = Arrays.asList(images);
        Collections.shuffle(random);
        
        getImage1().setImageDrawable(random.get(0));
        getImage2().setImageDrawable(random.get(1));
        
    	if(mp!=null) {
    		mp.release();
    	}
        playSound();
    }
	
    private Dialog getDlgCorrect() {
    	if(dlgCorrect == null) {
    		dlgCorrect = new Dialog(Quiz.this);
    		dlgCorrect.setTitle("   " + this.getResources().getString(R.string.correct));
    		dlgCorrect.setContentView(R.layout.result);
            ImageView img = (ImageView) dlgCorrect.findViewById(R.id.imgStatus);
            img.setImageResource(R.drawable.correct);
    	}
    	return dlgCorrect;
    }
    
    private Dialog getDlgWrong() {
    	if(dlgWrong == null) {
    		dlgWrong = new Dialog(Quiz.this);
    		dlgWrong.setTitle("   " + this.getResources().getString(R.string.wrong));
    		dlgWrong.setContentView(R.layout.result);
            ImageView img = (ImageView) dlgWrong.findViewById(R.id.imgStatus);
            img.setImageResource(R.drawable.wrong);
    	}
    	return dlgWrong;
    }
    
	private void playSound() {
		if(Home.soundEnabled) {
			try {
		        mp = MediaPlayer.create(this, animalsList.get(index).getSound());
	    		if(mp!=null) {
					mp.start();
	    		}
	        }catch(Exception e) {
	        	Log.e(Tutorial.class.getName(), e.toString());
            }
		}
    }
	
	private int getRandomDrawable() {
		int size = animalsList.size();
		int item = 0;
		while((item = new Random().nextInt(size)) == index); 
		return animalsList.get(item).getImage();
	}
	
    private void initBtnGameList() {
    	if(btnGameList == null) {
    		btnGameList = (Button) findViewById(R.id.btnGameList);
    		btnGameList.setOnClickListener(this);
    	}
    }

    private void initBtnSpeaker() {
    	if(btnSpeaker == null) {
    		btnSpeaker = (Button) findViewById(R.id.btnSpeaker);
    		btnSpeaker.setOnClickListener(this);
    		btnSpeaker.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(Home.soundEnabled ? R.drawable.speaker : R.drawable.dspeaker), null, null);
    	}
    }

    private void initBtnNext() {
    	if(btnNext == null) {
    		btnNext = (Button) findViewById(R.id.btnNext);
    		btnNext.setOnClickListener(this);
    	}
    }
    
}
